package com.kevin.Code.OfferReviewVersion1;

/**
 * @author Vinlee Xiao
 * @Classname OfferI_53
 * @Description 剑指 Offer 53 - II. 0～n-1中缺失的数字 二刷 利用二分查找
 * @Date 2022/2/26 10:51
 * @Version 1.0
 */
public class OfferI_53 {

    /**
     * 利用二分查找
     *
     * @param nums
     * @return
     */
    public int missingNumber(int[] nums) {
        int i = binarySearchMisssingNumber(nums, 0, nums.length - 1);
        return i;
    }

    /**
     * @param nums
     * @param low
     * @param high
     * @return
     */
    public int binarySearchMisssingNumber(int[] nums, int low, int high) {

        while (low <= high) {
            int mid = (low + high) >> 1;
            if (nums[mid] > mid) {
                high = mid - 1;
            } else {
                low = mid + 1;
            }
        }
        return low;
    }

    public static void main(String[] args) {
        int[] nums = new int[]{0, 1, 2, 3, 4, 5, 6, 7, 9};
        OfferI_53 offerI_53 = new OfferI_53();
        int i = offerI_53.binarySearchMisssingNumber(nums, 0, nums.length - 1);
        System.out.println(i);
    }
}
